package com.farm.shiro.util;

import com.farm.shiro.entity.Account;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;

/**
 * ShiroUtils
 *
 * @author ctidy
 * @since 2021/1/7
 */
public class ShiroUtils {

    private ShiroUtils(){ }

    /**
     * 获取当前用户Session
     * @return Session 用户信息
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    public static void login(AuthenticationToken token) {
        SecurityUtils.getSubject().login(token);
    }

    public static void login(String username, String plainPassword) {
        login(new UsernamePasswordToken(username, plainPassword));
    }

    /**
     * 用户登出
     */
    public static void logout() {
        SecurityUtils.getSubject().logout();
    }

    /**
     * 获取当前用户信息
     * @return Account 用户信息
     */
    public static Account getUserInfo() {
        return (Account) SecurityUtils.getSubject().getPrincipal();
    }

}
